package com.sxm.yiwei.activitis;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.sxm.yiwei.MainActivity;
import com.sxm.yiwei.R;
import com.sxm.yiwei.adapters.MsgListAdapter;
import com.sxm.yiwei.beans.SqBean;
import com.sxm.yiwei.tools.MySQManager;
import com.sxm.yiwei.tools.MySqHelper;
import com.sxm.yiwei.tools.SqData;

import java.util.ArrayList;
import java.util.List;

public class MsgMsgActivity extends BaseActivity implements View.OnClickListener{
    private String TAG ="tag";
    private ImageView msgback;
    private PullToRefreshListView listview;
    private List<Integer> list;
    private MySqHelper helper;
    private ImageView nodata;
    private ImageView backmain;

    List<SqBean> data = new ArrayList<>();
    private MsgListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_msg_msg);

        initMsgView();
        getSqData();
        if (data.size()==0){

        }else {
            nodata.setVisibility(View.GONE);
        }
        listview.setMode(PullToRefreshBase.Mode.BOTH);
        adapter = new MsgListAdapter(MsgMsgActivity.this, data);
        listview.setAdapter(adapter);
        adapter.notifyDataSetChanged();


    }

    private void getSqData() {
        listview.onRefreshComplete();
        helper = MySQManager.getInstance(MsgMsgActivity.this);
        SQLiteDatabase db = helper.getWritableDatabase();
        Cursor cursor = db.query("yivim", null, null, null, null, null, SqData.ID+" desc ");
        while (cursor.moveToNext()){
            String title = cursor.getString(cursor.getColumnIndex(SqData.TITLE));
            int id = cursor.getInt(cursor.getColumnIndex(SqData.ID));

            String time = cursor.getString(cursor.getColumnIndex(SqData.TIME));

            SqBean bean = new SqBean();
            bean.setId(id);
            bean.setTilte(title);

            bean.setTime(time);
            data.add(bean);
        }

        db.close();

    }

    private void initMsgView() {
        msgback = (ImageView) findViewById(R.id.message_msgback);
        listview = (PullToRefreshListView) findViewById(R.id.msg_listview);
        nodata = (ImageView) findViewById(R.id.msgmsg_nodata);
        backmain = (ImageView) findViewById(R.id.msgmsgm_backmain);
        backmain.setOnClickListener(this);
        list = new ArrayList<>();
        msgback.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.message_msgback:
                finish();
                break;
            case R.id.msgmsgm_backmain:

                startActivity(new Intent(MsgMsgActivity.this, MainActivity.class));
                AppManager.getAppManager().finishAllActivity();
                break;
        }
    }
}
